package com.hetang.maijiayun.adaptertools.adapter;

import com.hetang.maijiayun.adaptertools.b2cinterface.dangdang.client.DdClient;
import com.hetang.maijiayun.adaptertools.b2cinterface.dangdang.domain.OrderInfo;
import com.hetang.maijiayun.adaptertools.b2cinterface.dangdang.request.DdGetOrderRequest;
import com.hetang.maijiayun.adaptertools.b2cinterface.dangdang.response.DdGetOrderResponse;
import com.hetang.maijiayun.adaptertools.controller.vo.request.DetectorRequest;
import com.hetang.maijiayun.adaptertools.utils.JsonTools;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.http.impl.cookie.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by wanda on 11/4/15.
 */
public class DangdangAdapter extends AdapterAbstract {
    /**
     * 日志
     */
    private static final Logger logger = LoggerFactory.getLogger(DangdangAdapter.class);

    @Override
    public Integer request(Map<String, String> authorizesMap, DetectorRequest request) throws Exception {
        return null;
    }

    @Override
    public List<String> requestOrders(Map<String, String> authorizesMap, DetectorRequest request) throws Exception {
        if (MapUtils.isEmpty(authorizesMap) || !authorizesMap.containsKey("url") || !authorizesMap.containsKey("appKey") || !authorizesMap.containsKey("secret") || !authorizesMap.containsKey("sessionKey")) {
            logger.error("该当当平台店铺没有授权信息或授权信息错误！！");
            return null;
        }
        DdClient client = new DdClient(authorizesMap.get("url"), authorizesMap.get("appKey"), authorizesMap.get("secret"), authorizesMap.get("sessionKey"));
        DdGetOrderRequest req = new DdGetOrderRequest(2);
        req.setStartPoint(DateUtils.formatDate(request.getStartTime(), "yyyy-MM-dd HH:mm:ss"));
        req.setEndPoint(DateUtils.formatDate(request.getEndTime(), "yyyy-MM-dd HH:mm:ss"));
        req.setPageNo(1);
        req.setPageSize(20);
        req.setOrderState("9999");
        logger.info("dangdang get order interface, request time is : {}, request param is : {}, author : {}", System.currentTimeMillis(), JsonTools.createJsonString(req), JsonTools.createJsonString(authorizesMap));
        DdGetOrderResponse rsp = client.execute(req);
        logger.info("dangdang get order interface, response time is : {}, response : {}", System.currentTimeMillis(), JsonTools.createJsonString(rsp));
        if (rsp == null || rsp.getOrdersList() == null ) {
            return null;
        }
        Long page = 0L;
        Long mod = rsp.getTotalInfo().getOrderCount()%20L;
        page = rsp.getTotalInfo().getOrderCount()/20L;
        if (0 != mod) {
            page += 1;
        }
        List<String> result = new ArrayList<String>();
        while (page > 0) {
            req.setPageNo(page.intValue());
            rsp = client.execute(req);
            if (rsp.getOrdersList() == null ) {
                logger.error("dangdang get order interface error, response time is : {}, response : {}", System.currentTimeMillis(), JsonTools.createJsonString(rsp));
                continue;
            }
            if (CollectionUtils.isEmpty(rsp.getOrdersList())) {
                continue;
            }
            for (OrderInfo orderInfo : rsp.getOrdersList()) {
                result.add(orderInfo.getOrderID() + "");
            }
            page -- ;
        }
        return result;
    }
}
